<?php
class FeedController extends AppController {
    // var $helpers = array ('Html','Form');
    //errore:var $name = 'Pages'; // riferimento alla cartella delle view
    
    public function beforeFilter(){
    	
		parent::beforeFilter();
        $this->Auth->allow('getLive','getLiveScore','getOdds');
		
    }
    
    public function index($type=null) {
    	if ($type!=null) {
			$feeds = $this->Feed->find("all",array('conditions'=>array('Feed.type'=>$type)));
			$this->set("feeds",$feeds);
    	}
    }

	public function getOdds($country) {
		$this->layout="ajax";
		$this->autorender = false;
		if($this->request->is('ajax')) {
			try {
				$feed = $this->Feed->find("first",array("recursive"=>-1, "fields"=>array("Feed.url"), "conditions"=>array("Feed.country"=>$country,"Feed.type"=>FEED_TYPE_SOCCER_ODDS)));
				// Open the Curl session
				$session = curl_init($feed["Feed"]["url"]);
				// Don't return HTTP headers. Do return the contents of the call
				curl_setopt($session, CURLOPT_HEADER, false);
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				// Make the call
				$xml = curl_exec($session);
				// The web service returns XML. Set the Content-Type appropriately
				header("Content-Type: text/xml");
				echo $xml;
				curl_close($session);
				die;
				
				// $data = array();
				// $data["url"] = $feed["Feed"]["url"];
				// $data["success"] = true;
				// echo json_encode($data);
				// die();
			} catch (Exception $e) {
			    CakeLog::error($e->getMessage());
				$data = array('success'=> false,'message'=>$e->getMessage());
				echo json_encode($data);
				die();
			}
		} else {
            throw new MethodNotAllowedException(__("Chiamata errata."));
		}
	}

	public function getLiveScore($country) {
		$this->layout="ajax";
		$this->autorender = false;
		if($this->request->is('ajax')) {
			try {
				$feed = $this->Feed->find("first",array("recursive"=>-1, "fields"=>array("Feed.url"), "conditions"=>array("Feed.country"=>$country,"Feed.type"=>FEED_TYPE_SOCCERNEW)));
				// Open the Curl session
				$session = curl_init($feed["Feed"]["url"]);
				// Don't return HTTP headers. Do return the contents of the call
				curl_setopt($session, CURLOPT_HEADER, false);
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				// Make the call
				$xml = curl_exec($session);
				// The web service returns XML. Set the Content-Type appropriately
				header("Content-Type: text/xml");
				echo $xml;
				curl_close($session);
				die;
				
				// $data = array();
				// $data["url"] = $feed["Feed"]["url"];
				// $data["success"] = true;
				// echo json_encode($data);
				// die();
			} catch (Exception $e) {
			    CakeLog::error($e->getMessage());
				$data = array('success'=> false,'message'=>$e->getMessage());
				echo json_encode($data);
				die();
			}
		} else {
            throw new MethodNotAllowedException(__("Chiamata errata."));
		}
	}

	public function getLive($country) {
		$this->layout="ajax";
		$this->autorender = false;
		if($this->request->is('ajax')) {
			try {
                // $this->loadModel('Feed');
				$feed = $this->Feed->find("first",array("recursive"=>-1, "fields"=>array("Feed.url"), "conditions"=>array("Feed.country"=>$country,"Feed.type"=>FEED_TYPE_COMMENTARIES)));
				
				// Open the Curl session
				$session = curl_init($feed["Feed"]["url"]);
				// Don't return HTTP headers. Do return the contents of the call
				curl_setopt($session, CURLOPT_HEADER, false);
				curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
				// Make the call
				$xml = curl_exec($session);
				// The web service returns XML. Set the Content-Type appropriately
				header("Content-Type: text/xml");
				echo $xml;
				curl_close($session);
				die;

				// $data = array();
				// $data["url"] = $feed["Feed"]["url"];
				// $data["success"] = true;
				// echo json_encode($data);
				// die();
			} catch (Exception $e) {
			    CakeLog::error($e->getMessage());
				$data = array('success'=> false,'message'=>$e->getMessage());
				echo json_encode($data);
				die();
			}
		} else {
            throw new MethodNotAllowedException(__("Chiamata errata."));
		}
	}
	
	public function getCommentaries($country) {
		$this->layout="ajax";
		$this->autorender = false;
		if($this->request->is('ajax')) {
			try {
                // $this->loadModel('Feed');
				$data = array();
				$feed = $this->Feed->find("first",array("recursive"=>-1, "fields"=>array("Feed.url"), "conditions"=>array("Feed.country"=>$country,"Feed.type"=>FEED_TYPE_COMMENTARIES)));
				$data["url"] = $feed["Feed"]["url"];
				$data["success"] = true;
				echo json_encode($data);
				die();
			} catch (Exception $e) {
			    CakeLog::error($e->getMessage());
				$data = array('success'=> false,'message'=>$e->getMessage());
				echo json_encode($data);
				die();
			}
		} else {
            throw new MethodNotAllowedException(__("Chiamata errata."));
		}
	}
	

}
?>